package com.bdqn.tour.servlet.Hqf;

import com.bdqn.tour.pojo.Scenic;
import com.bdqn.tour.service.HqfupdateService;
import com.bdqn.tour.service.impl.hqfupdateServiceImpl;
import com.bdqn.tour.util.MyConfig;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

//修改景点信息servlet
@WebServlet("/hqfUpdateScenic")
@MultipartConfig
public class HqfupdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //设置编码格式
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //获取对象参数
        String scenicname = req.getParameter("updatescenicname");
        String scenicaddress = req.getParameter("updateaddress");
        String prices = req.getParameter("updateprice");
        String satisfactions = req.getParameter("updatesatisfaction");
        String city = req.getParameter("updatecity");
        String html = req.getParameter("updatehtml");
        int id = Integer.parseInt(req.getParameter("id"));

        // 验证参数
        if (scenicname == null || scenicname.trim().isEmpty() ||
                scenicaddress == null || scenicaddress.trim().isEmpty() ||
                prices == null || prices.trim().isEmpty() ||
                satisfactions == null || satisfactions.trim().isEmpty() ||
                city == null || city.trim().isEmpty() ||
                html == null || html.trim().isEmpty()) {
            resp.getWriter().write("参数错误");
            return;
        }
        //转换数据类型为整型
        int price = Integer.parseInt(prices);
        int satisfaction = Integer.parseInt(satisfactions);

            //处理图片上传
            String img = "";
            Part imgPart = req.getPart("updatepicture");   //获取文件请求对象

            //限制文件大小
            long size = imgPart.getSize();
            if (size > 0 && size < 5 * 1024 * 1024) {
                String contentType = imgPart.getContentType();   //获取图片类型
                if (contentType.startsWith("image")) {    //startwith图片image开头

                    String fileName = imgPart.getSubmittedFileName();   //返回文件名
                    //获取文件后缀
                    String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
                    //新文件名  自行定义
                    String newFileName = UUID.randomUUID() + fileSuffix;
                    //创建文件对象
                    String filePath = MyConfig.getValue("filePath");
                    try {
                        imgPart.write(filePath + File.separatorChar + newFileName);
                        img = newFileName;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("imgPart:上传的不是图片格式");
                }
            } else {
                System.out.println("imgPart:不符合大小");
            }
            //创建对象scenic
            Scenic scenic = new Scenic();
            scenic.setScenic_name(scenicname);
            scenic.setScenic_address(scenicaddress);
            scenic.setPrice(price);
            scenic.setImg(img);
            scenic.setSatisfaction(satisfaction);
            scenic.setCity(city);
            scenic.setHtml(html);
            scenic.setScenic_Id(id);
            //业务层hqfupdateService方法调用
            HqfupdateService service = new hqfupdateServiceImpl();
            int count = service.updateScenic(scenic);
            if (count > 0) {
                System.out.println("修改成功");
                //转发到查询hqfSearch
                req.getRequestDispatcher("/hqfSearch").forward(req, resp);
            } else {
                resp.getWriter().write("修改失败");
            }

        }
    }
